<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
    <title>五道</title>
    <style>
        body {
            background-color: transparent;
        }

        canvas {
            display: block;
            margin: 50px auto;
            box-shadow: -2px -2px 2px #EFEFEF, 5px 5px 5px #B9B9B9;
            cursor: pointer;
        }
    </style>
</head>

<body>

    <canvas id="chess" width="500px" height="500px">
    </canvas>
    <div style="margin: 50px auto;"><span>准备</span> <span>开始</span> <span>悔棋</span></div>
    <div id="round" style="margin: 50px auto;"></div>
</body>
<script>
    let chess = document.getElementById("chess");
    let context = chess.getContext('2d');
    let rect = chess.getBoundingClientRect();
    context.strokeStyle = '#bfbfbf';	//边框颜色
    let chressBord = []; //棋盘
    const whitechress = 10;
    const blackchress = 20;
    const whiteactive = 11;
    const blackactive = 21;
    const nochress = 0;
    const offset = 50;
    const lineSpace = 100;
    const chressSize = 30;
    let round = {};
    Object.defineProperties(round, {
        isWhite: {
            configurable: true,
            get: function () {
                return isWhite;
            },
            set: function (newValue) {
                isWhite = newValue;
                document.getElementById("round").innerText = newValue ? '白棋回合' : '黑棋回合';
            }
        }
    });
    round.isWhite = true;
    let activeChress = false;
    let ax;
    let ay;
    let ac;
    for (let x = 0; x < 5; x++) {
        chressBord[x] = [];
        for (let y = 0; y < 5; y++) {
            if (y == 0) {
                chressBord[x][y] = whitechress;
            } else if (y == 4) {
                chressBord[x][y] = blackchress;
            } else {
                chressBord[x][y] = nochress;
            }
        }
    }
    window.onload = function () {
        drawChessBoard(); // 画棋盘
        drawChress();//画棋子
        context.save();
    };

    chess.onclick = function (e) {
        let x = Math.floor(e.clientX - rect.left);
        let y = Math.floor(e.clientY - rect.top);
        let xx = Math.floor(Math.abs((x)) / lineSpace);
        let yy = Math.floor(Math.abs((y)) / lineSpace);
        if (activeChress) {
            //黑棋取消选中
            if (chressBord[xx][yy] == blackactive) {
                chressBord[xx][yy] = blackchress;
                activeChress = false;
                //白棋取消选中
            } else if (chressBord[xx][yy] == whiteactive) {
                chressBord[xx][yy] = whitechress;
                activeChress = false;
            } else if (chressBord[xx][yy] == blackchress) {
                chressBord[xx][yy] = blackactive;
                chressBord[ax][ay] = ac == whitechress ? whitechress : blackchress;
                activeChress = true;
                ac = blackchress;
                ax = xx;
                ay = yy;
            } else if (chressBord[xx][yy] === whitechress) {
                chressBord[xx][yy] = whiteactive;
                chressBord[ax][ay] = ac === whitechress ? whitechress : blackchress;
                activeChress = true;
                ac = whitechress;
                ax = xx;
                ay = yy;
            } else {
                if (round.isWhite ^ (ac === whitechress)) {
                    //走棋  一次只能走一步
                    if (Math.abs(ax - xx) + Math.abs(ay - yy) === 1) {
                        chressBord[ax][ay] = nochress;
                        chressBord[xx][yy] = ac === whitechress ? whitechress : blackchress;
                        activeChress = false;
                        round.isWhite = !round.isWhite;
                        horizontalEat(xx, yy);
                        verticalEat(xx, yy);
                    } else {
                        alert("一次只能走一步");
                    }
                } else {
                    alert("这回合不是你的回合");
                }
            }
        } else {
            //如果没有棋子选中，那么说明现在在选棋子
            //如果选中黑棋
            if (chressBord[xx][yy] === blackchress) {
                chressBord[xx][yy] = blackactive;
                activeChress = true;
                ac = blackchress;
                ax = xx;
                ay = yy;
            }
            //如果选中白棋
            if (chressBord[xx][yy] === whitechress) {
                chressBord[xx][yy] = whiteactive;
                activeChress = true;
                ac = whitechress;
                ax = xx;
                ay = yy;
            }
        }
        clearCanvas();
        drawChessBoard();
        drawChress();
    }
    //绘画棋盘
    var drawChessBoard = function () {
        for (var i = 0; i < 5; i++) {
            context.moveTo(offset + i * lineSpace, offset);
            context.lineTo(offset + i * lineSpace, 450);
            context.stroke();
            context.moveTo(offset, offset + i * lineSpace);
            context.lineTo(450, offset + i * lineSpace);
            context.stroke();
        }
    }

    function drawChress() {
        for (let x = 0; x < 5; x++) {
            for (let y = 0; y < 5; y++) {
                if (chressBord[x][y] == whitechress) {
                    oneStep(x, y, true, false);
                }
                if (chressBord[x][y] == blackchress) {
                    oneStep(x, y, false, false);
                }
                if (chressBord[x][y] == whiteactive) {
                    oneStep(x, y, true, true);
                }
                if (chressBord[x][y] == blackactive) {
                    oneStep(x, y, false, true);
                }
            }
        }
    }

    //画棋子
    var oneStep = function (i, j, isWhite, isActive) {
        context.beginPath();
        context.arc(offset + i * lineSpace, offset + j * lineSpace, chressSize, 0, 2 * Math.PI);// 画圆
        context.closePath();
        //渐变
        let gradient = context.createRadialGradient(offset + i * lineSpace
            + 2, offset + j * lineSpace - 2, chressSize, offset + i * lineSpace + 2, offset + j * lineSpace -
        2, 0);
        if (isWhite) {
            gradient.addColorStop(0, '#0a0a0a');
            gradient.addColorStop(1, '#636766');
        } else {
            gradient.addColorStop(0, '#d1d1d1');
            gradient.addColorStop(1, '#f9f9f9');
        }
        context.fillStyle = gradient;
        context.fill();
        context.restore();
        if (isActive) {
            context.rect(offset + i * lineSpace - chressSize,
                offset + j * lineSpace - chressSize,
                offset + i * lineSpace + chressSize - (offset + i * lineSpace - chressSize),
                offset + j * lineSpace + chressSize - (offset + j * lineSpace - chressSize));
            //context.setLineDash([5, 15, 5]);
            //context.lineCap = "round";
            context.stroke();
            context.restore();
        }
    };

    function clearCanvas() {
        //擦除该圆
        context.clearRect(0, 0, 500, 500);
        context.stroke();
        context.restore();
    }

    function rule() {
        whiteCount = 0;
        blackCount = 0;
        for (let i = 0; i <= 4; i++) {
            for (let j = 0; j <= 4; j++) {
                if (chressBord[i][j] == blackchress || chressBord[i][j] == blackactive) {
                    blackCount++;
                }
                if (chressBord[i][j] == whitechress || chressBord[i][j] == whiteactive) {
                    whiteCount++;
                }
            }
        }
        if (whiteCount < 2) {
            alert("黑棋赢");
            //location.reload();
        }
        if (blackCount < 2) {
            alert("白棋赢");
            //location.reload();
        }
    }
    //检测横向吃子,y固定
    function horizontalEat(x, y) {
        let blackCount = 0;
        let whiteCount = 0;
        let min = -1;
        let max = -1;
        let whiteX = -1;
        let blackX = -1;
        for (let i = 0; i <= 4; i++) {
            if (chressBord[i][y] == nochress) {
                continue;
            }
            if (chressBord[i][y] == blackchress) {
                blackCount++;
                if (min == -1) {
                    min = i;
                }
                if (max < i) {
                    max = i;
                }
                blackX = i;
            }
            if (chressBord[i][y] == whitechress) {
                whiteCount++;
                if (min == -1) {
                    min = i;
                }
                if (max < i) {
                    max = i;
                }
                whiteX = i;
            }
        }
        if (chressBord[min][y] == chressBord[max][y]) {
            return;
        }
        if (min + 2 != max) {
            return;
        }
        if ((blackCount + whiteCount != 3) || (blackCount * whiteCount != 2)) {
            return;
        }
        if (blackCount > whiteCount) {
            if (!round.isWhite) {
                chressBord[whiteX][y] = nochress
            }
        } else {
            if (round.isWhite) {
                chressBord[blackX][y] = nochress
            }
        }
    }

    //检测纵向吃子 x固定
    function verticalEat(x, y) {
        let blackCount = 0;
        let whiteCount = 0;
        let min = -1;
        let max = -1;
        let whiteY = -1;
        let blackY = -1;
        for (let i = 0; i <= 4; i++) {
            if (chressBord[x][i] == nochress) {
                continue;
            }
            if (chressBord[x][i] == blackchress) {
                blackCount++;
                if (min == -1) {
                    min = i;
                }
                if (max < i) {
                    max = i;
                }
                blackY = i;
            }
            if (chressBord[x][i] == whitechress) {
                whiteCount++;
                if (min == -1) {
                    min = i;
                }
                if (max < i) {
                    max = i;
                }
                whiteY = i;
            }
        }
        if (chressBord[x][min] == chressBord[x][max]) {
            return;
        }
        if (min + 2 != max) {
            return;
        }
        if ((blackCount + whiteCount != 3) || (blackCount * whiteCount != 2)) {
            return;
        }
        if (blackCount > whiteCount) {
            if (!round.isWhite) {
                chressBord[x][whiteY] = nochress
            }
        } else {
            if (round.isWhite) {
                chressBord[x][blackY] = nochress
            }
        }
    }
</script>

</html>